package com.namuan.jp_stock.util;

import java.util.ArrayList;

import com.namuan.jp_stock.object.Timestamp;
import com.namuan.jp_stock.object.Trackback;
import com.namuan.jp_stock.R;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class TrackbackItemAdapter extends ArrayAdapter<Trackback> {
	private int mResource;
	private ArrayList<Trackback> mList;
	private LayoutInflater mInflater;
	private Activity activity;
	public CustomImageManager imageManager;
	/**
	 * @param context
	 * @param layoutResource
	 * @param objects
	 */
	public TrackbackItemAdapter(Activity a, int layoutResource, ArrayList<Trackback> objects)
	{
		super(a, layoutResource, objects);
		this.activity =a;
		this.mResource = layoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageManager = 
			new CustomImageManager(activity.getApplicationContext(),R.drawable.defaultuser);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		
		convertView = mInflater.inflate(mResource, null);
		ProfileImageView trackbackUserImg=(ProfileImageView) convertView.findViewById(R.id.trackbackUserImg);
		TextView trackbackUserNickname = (TextView) convertView.findViewById(R.id.trackbackUserNickname);
		TextView trackbackComment = (TextView) convertView.findViewById(R.id.trackbackComment);
		//TextView trackbackSummary = (TextView) convertView.findViewById(R.id.trackbackSummary);
		TextView trackbackTimeStamp = (TextView) convertView.findViewById(R.id.trackbackTimeStamp);
		
		TextView trackbackRecommendation = (TextView) convertView.findViewById(R.id.trackbackRecommendation);
		

		Trackback Trackback = mList.get(position);
		if (Trackback.getProfilepic() != null && !Trackback.getProfilepic().equals("null")) {
			trackbackUserImg.setTag(Justplace.PROFILEIMG_URL + Trackback.getProfilepic());
			Log.d("!!!!!!!!!!!!!!!!!!!!!!!!", Justplace.PROFILEIMG_URL + Trackback.getProfilepic());
			imageManager.displayImage(Justplace.PROFILEIMG_URL + Trackback.getProfilepic(), activity, trackbackUserImg);
		} else {
			trackbackUserImg.setTag(null);
			imageManager.displayImage(Trackback.getProfilepic(), activity, trackbackUserImg);
		}
		
		trackbackUserNickname.setText(Trackback.getUsernickname());
		Integer recommendation = Trackback.getRecommendation();
		
		trackbackRecommendation.setText(recommendation.toString());

		if( Trackback.isTrackbackpw() == false ) {
			trackbackUserNickname.setTextColor(R.color.ltgray);
		}
		
		
		trackbackComment.setText(Trackback.getComment());
		trackbackTimeStamp.setText(new Timestamp().timestmsp(Trackback.getTimestamp()));
		
		/*************** **********************
		 * *************** **********************
		 * *************** **********************
		 *************** ***********************/
		boolean ismemeber = Trackback.isTrackbackpw();
		
		/*************** **********************
		 * *************** **********************
		 * *************** **********************
		 * *************** **********************
		 * *************** **********************/
		
		return convertView;
	}


	public class ActivityViewHolder {
	  public ProfileImageView trackbackUserImg;
	  public TextView trackbackUserNickname;
	  public TextView trackbackComment;
	  public TextView trackbackSummary;
	  public RatingBar trackbackRating;
	  public TextView trackbackTimeStamp;		
	}
}
